from django.db import models

from apps.test_projects.models import TestEnv, TestProject
from apps.test_tasks.models import TestTask
from utils.models import CoreModel, table_prefix


# Create your models here.
class CronJob(CoreModel):
    """定时任务表"""
    project = models.ForeignKey(
        to=TestProject, on_delete=models.CASCADE,
        help_text='项目id', verbose_name='项目id', db_comment='项目id'
    )
    name = models.CharField(
        max_length=150,
        help_text='名称', verbose_name='名称', db_comment='名称'
    )
    task = models.ForeignKey(
        to=TestTask, on_delete=models.PROTECT,
        help_text='执行任务', verbose_name='执行任务', db_comment='执行任务',
    )
    rule = models.CharField(
        max_length=80, default='* * * * *',
        help_text='定时执行规则', verbose_name='定时任务', db_comment='定时执行规则'
    )
    status = models.BooleanField(
        default=False,
        help_text='状态', verbose_name='状态', db_comment='状态'
    )
    env = models.ForeignKey(
        to=TestEnv, on_delete=models.PROTECT,
        help_text='执行环境', verbose_name='执行环境', db_comment='执行环境',
    )

    def __str__(self):
        return self.name

    class Meta:
        db_table = table_prefix + 'cron_job'
        db_table_comment = "定时任务表"

        verbose_name = "定时任务表"
        verbose_name_plural = verbose_name
